﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;

namespace RewrittingUrlDemo
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            //Khoi tao rule

            UrlRewritingNet.Web.RegExRewriteRule ruleCategory = new UrlRewritingNet.Web.RegExRewriteRule();

            UrlRewritingNet.Web.RegExRewriteRule ruleProduct = new UrlRewritingNet.Web.RegExRewriteRule();



            //Rule – rewrite cho Products

            ruleProduct.VirtualUrl = "^~/(.*)/(.*)/(.*).html";

            ruleProduct.DestinationUrl = "~/UI/Product.aspx?CategoryID=$1&amp;ProductID=$2";

            ruleProduct.IgnoreCase = true;

            ruleProduct.Rewrite = UrlRewritingNet.Web.RewriteOption.Application;

            ruleProduct.Redirect = UrlRewritingNet.Web.RedirectOption.None;

            ruleProduct.RewriteUrlParameter = UrlRewritingNet.Web.RewriteUrlParameterOption.ExcludeFromClientQueryString;

            UrlRewritingNet.Web.UrlRewriting.AddRewriteRule("Product_Rule", ruleProduct);



            //Rule – rewrite cho Categories

            ruleCategory.VirtualUrl = "^~/(.*)/(.*).html"; //1-CatID, 2-CatName

            ruleCategory.DestinationUrl = "~/UI/Category.aspx?CategoryID=$1";

            ruleCategory.IgnoreCase = true;

            ruleCategory.Rewrite = UrlRewritingNet.Web.RewriteOption.Application;

            ruleCategory.Redirect = UrlRewritingNet.Web.RedirectOption.None;

            ruleCategory.RewriteUrlParameter = UrlRewritingNet.Web.RewriteUrlParameterOption.ExcludeFromClientQueryString;

            UrlRewritingNet.Web.UrlRewriting.AddRewriteRule("Category_Rule", ruleCategory);
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}